package com.nanhang.service.impl;


import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nanhang.entity.Specification;
import com.nanhang.entity.SpecificationExample;
import com.nanhang.entity.SpecificationOption;
import com.nanhang.entity.SpecificationOptionExample;
import com.nanhang.mapper.SpecificationMapper;
import com.nanhang.mapper.SpecificationOptionMapper;
import com.nanhang.service.SpecificationService;
import com.nanhang.util.PageResult;
import com.nanhang.vo.SpecificationOptionVO;
import com.nanhang.vo.SpecificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private SpecificationMapper specificationMapper;


    @Override
    public List<Specification> findSpecification() {
        return specificationMapper.selectByExample(null);
    }

    @Override
    public PageResult<Specification> findByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Specification> specifications = this.findSpecification();
        Page<Specification> specificationPage = (Page<Specification>) specifications;
        return new PageResult<>(specificationPage.getResult(),specificationPage.getTotal());
    }

    @Override
    public PageResult<Specification> search(Integer page, Integer pageSize, SpecificationVO specificationVO) {

        PageHelper.startPage(page, pageSize);
        SpecificationExample example = new SpecificationExample();

        SpecificationExample.Criteria criteria = example.createCriteria();
        if(StrUtil.isNotBlank(specificationVO.getSpecName())){
            criteria.andSpecNameLike("%" + specificationVO.getSpecName() + "%");
        }
        List<Specification> specifications = specificationMapper.selectByExample(example);
        Page<Specification> specificationPage = (Page<Specification>) specifications;
        return new PageResult<>(specificationPage.getResult(),specificationPage.getTotal());
    }

    @Override
    public boolean save(Specification specification) {
        return specificationMapper.insert(specification) > 0;
    }


}
